﻿using System;
using System.Collections.Generic;
using System.Linq;
using WeChatCmsCommon.EnumBusiness;
using WeChatDataAccess;
using WeChatModel.CustomerModel;
using WeChatModel.DatabaseModel;

namespace WeChatService
{
    public class DailyHistoryService
    {
        /// <summary>
        /// 数据服务
        /// </summary>
        private readonly DailyHistoryData _dataAccess = new DailyHistoryData();

        /// <summary>
        /// 资源服务
        /// </summary>
        private readonly DailyStoryResourceService _resourceServer = new DailyStoryResourceService();

        /// <summary>
        /// 审核记录服务
        /// </summary>
        private readonly DailyverifydetailData _dailyverifydetailData = new DailyverifydetailData();

        /// <summary>
        /// 审核人信息
        /// </summary>
        private readonly DailyverifylistData _dailyverifylistData = new DailyverifylistData();


        /// <summary>
        /// 保存数据
        /// </summary>
        /// <param name="id">主键</param>
        /// <param name="number">工作量</param>
        /// <param name="dailyDateTime">工作时间</param>
        /// <param name="dailyContent">工作内容</param>
        /// <param name="userId">用户id</param>
        /// <param name="userName">申请人姓名</param>
        /// <param name="dailyType">申请类型</param>
        /// <param name="verifyId">审核人id</param>
        /// <returns></returns>
        public long SaveModel(long id, decimal number, DateTime dailyDateTime, string dailyContent, long userId,
            string userName, DailyTypeEnum dailyType, long verifyId)
        {
            var model = new DailyHistoryModel();
            if (id > 0)
            {
                model = _dataAccess.Get(id);
            }
            else
            {
                model.Createtime = DateTime.Now;
                model.DealType = DealTypeEnum.HandSubmit;
            }

            model.DailyDate = dailyDateTime;
            model.DailyContent = dailyContent.Length > 500 ? dailyContent.Substring(0, 500) : dailyContent;
            model.DailyMonth = dailyDateTime.Month;
            model.DailyNumber = number;
            model.DailyYear = dailyDateTime.Year;
            model.IsDel = FlagEnum.HadZore;
            model.UpdateTime = DateTime.Now;
            model.UserId = userId;
            model.DailyDay = dailyDateTime.Day;
            model.CreateUserName = userName;
            model.DailyType = dailyType;
            model.VerifyId = verifyId;
            model.VerifyState = VerifyStateEnum.ReadyDo;
            return _dataAccess.SaveModel(model);
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public long SaveModel(DailyHistoryModel model)
        {
            return _dataAccess.SaveModel(model);
        }

        /// <summary>
        /// 查询用户的日志信息
        /// </summary>
        /// <param name="userId">用户id</param>
        /// <param name="typeEnum">类型</param>
        /// <param name="year">年</param>
        /// <param name="month">月</param>
        /// <param name="day">天</param>
        /// <returns>日志记录</returns>
        public List<DailyHistoryModel> GetDailyHistoryModels(long userId, DailyTypeEnum? typeEnum, int year, int month,
            int day = 0)
        {
            var dailyList = _dataAccess.GetDailyHistoryListByUserId(userId, typeEnum, year, month, day);
            if (dailyList == null) return null;
            var resourceModels = _resourceServer.GetDailyStoryResourceModels(dailyList.Select(f => f.Id).ToList());
            if (resourceModels != null)
            {
                dailyList.ForEach(r =>
                    r.MediaList = resourceModels.Where(e => e.StoryDetailId == r.Id).OrderBy(s => s.Sort).ToList());
            }

            return dailyList;
        }

        /// <summary>
        /// 根据用户id获取剩余可用调休余额
        /// </summary>
        /// <param name="userId">用户id</param>
        /// <returns></returns>
        public Decimal GetRemainderHistoryByUserId(long userId)
        {
            if (userId < 1)
            {
                return 0;
            }

            return _dataAccess.GetRemainderHistoryByUserId(userId);
        }

        /// <summary>
        /// 根据id获取数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public DailyHistoryModel GetDailyHistoryDetailInfo(long id)
        {
            DailyHistoryModel model = _dataAccess.Get(id);
            if (model != null && model.IsDel == FlagEnum.HadOne)
            {
                model = null;
            }

            if (model == null) return null;
            var resourceList = _resourceServer.GetDailyStoryResourceModels(new List<long> { id });
            model.MediaList = resourceList;
            return model;
        }

        /// <summary>
        /// 获取有效的审核人列表
        /// </summary>
        /// <returns></returns>
        public List<DailyverifylistModel> GetDailyverifylistModels()
        {
            return _dailyverifylistData.GetDailyVerifyList();
        }

        /// <summary>
        /// 获取审核人信息
        /// </summary>
        /// <param name="verifyId">审核人id</param>
        /// <returns></returns>
        public DailyverifylistModel GetDailyverifylistModel(long verifyId)
        {
            return _dailyverifylistData.GetDailyVerifyModelByVerifyId(verifyId);
        }

        /// <summary>
        /// 保存信息审批记录
        /// </summary>
        /// <param name="saveModel"></param>
        public long SaveModelDailyverifydetailModel(DailyverifydetailModel saveModel)
        {
            return _dailyverifydetailData.SaveModel(saveModel);
        }

        /// <summary>
        /// 根据审核用户记录
        /// </summary>
        /// <param name="verifyId">审核人用户id</param>
        /// <returns>查询日志记录</returns>
        public List<DailyverifydetailModel> GetDailyVerifyDetail(long verifyId)
        {
            return _dailyverifydetailData.GetDailyVerifyDetail(verifyId);
        }

        /// <summary>
        /// 根据记录id查询还未审核的记录
        /// </summary>
        /// <param name="dailyId">申请id</param>
        /// <returns>查询日志记录</returns>
        public DailyverifydetailModel GetDailyVerifyDetailByDailyId(long dailyId)
        {
            return _dailyverifydetailData.GetDailyVerifyDetailByDailyId(dailyId);
        }

        /// <summary>
        /// 根据用户id获取待审核的记录
        /// </summary>
        /// <param name="verifyId">审核人</param>
        /// <param name="verifyStateEnum">审核状态</param>
        /// <param name="indexPage"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public List<DailyHistoryWithVerifyModel> GetReadyDoVerifyDailyModels(long verifyId, VerifyStateEnum verifyStateEnum,
            int indexPage, int pageSize, out int count)
        {
            count = _dailyverifydetailData.GetDailyHistoryModelsCount(verifyId, verifyStateEnum);
            if (count < 1)
            {
                return null;
            }

            var list = _dailyverifydetailData.GetDailyHistoryModels(verifyId, verifyStateEnum, indexPage, pageSize);
            if (list != null && list.Count > 0)
            {
                var resourceModels = _resourceServer.GetDailyStoryResourceModels(list.Select(f => f.Id).ToList());
                if (resourceModels != null)
                {
                    list.ForEach(r =>
                        r.MediaList = resourceModels.Where(e => e.StoryDetailId == r.Id).OrderBy(s => s.Sort).ToList());
                }
            }
            return list;
        }

        /// <summary>
        /// 获取待办数据
        /// </summary>
        /// <param name="verifyId"></param>
        /// <param name="readyDo"></param>
        /// <param name="hadDo"></param>
        public void GetReadyDoHadDoVerifyDailyHistory(long verifyId, out int readyDo, out int hadDo)
        {
            readyDo = _dailyverifydetailData.GetDailyHistoryModelsCount(verifyId, VerifyStateEnum.ReadyDo);
            hadDo = _dailyverifydetailData.GetDailyHistoryModelsCount(verifyId, VerifyStateEnum.Pass);
        }

        /// <summary>
        /// 获取待办数量
        /// </summary>
        /// <param name="verifyId">待处理人</param>
        /// <returns></returns>
        public int GetReadyDoVerifyCount(long verifyId)
        {
            return _dailyverifydetailData.GetDailyHistoryModelsCount(verifyId, VerifyStateEnum.ReadyDo);
        }
    }
}
